import { User, Answer, Task } from '../models'
import _ from 'lodash'

export const getSubmitedsByTaskId = async ({autherId, taskId}) => {
  // let task = await Task.findById(taskId)
  // if (!task) throw new Error(`${ERRORS.DBERR.ERR_RECORD_NOT_FOUND}`)
  // if (task.ownerId != autherId) throw new Error(`${ERRORS.DBERR.ERR_UNAUTHORIZED_OPERATION}`)

  let answers = await Answer.findAll({
    where: {
      taskId
    },
    raw: true
  })
  let userIds = _.map(answers, n => {
    return n.userId
  })
  let users = await User.findAll({
    attributes: ['id', 'nickName', 'avatarUrl'],
    where: {
      id: userIds
    },
    raw: true
  })
  return users
}
